package io.renren.modules.oss.cloud;

import io.renren.common.util.ConfigConstant;
import io.renren.common.util.Constant;
import io.renren.modules.sys.service.SysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 文件上传Factory
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-03-26 10:18
 */
@Component
public final class OSSFactory {

    @Autowired
    private SysConfigService sysConfigService;

    private CloudStorageService cloudStorageService;

    private Integer nowType;

    public CloudStorageService getCloudStorageService() {
        //获取云存储配置信息
        CloudStorageConfig config = sysConfigService.getConfigObject(ConfigConstant.CLOUD_STORAGE_CONFIG_KEY, CloudStorageConfig.class);

        if(cloudStorageService == null ||
                nowType == null ||
                !nowType.equals(config.getType())
                ){
            cloudStorageService = build(config);
            nowType = config.getType();
        }

        return cloudStorageService;
    }

    private CloudStorageService build(CloudStorageConfig config){

        if(config.getType() == Constant.CloudService.QINIU.getValue()){
            return new QiniuCloudStorageService(config);
        }else if(config.getType() == Constant.CloudService.ALIYUN.getValue()){
            return new AliyunCloudStorageService(config);
        }else if(config.getType() == Constant.CloudService.QCLOUD.getValue()){
            return new QcloudCloudStorageService(config);
        }

        return null;
    }

}
